#include <stdio.h>
#include <string.h>
#include <stdlib.h>

//定义需要使用数组的最大长度
#define MAX_USER_LEN 100
#define MAX_PWD_LEN 100
#define MAX_MENU_LEN 100
#define MAX_ISBN_LEN 100
#define MAX_BOOK_LEN 100
#define MAX_PRICE_LEN 100
#define MAX_PUBLISHER_LEN 100
#define MAX_AUTHOR_LEN 100

//共用体，可以定义不同变量
union Data{
    int bookCount;
    int userCount;
    int menuCount;
} Data;

int accountCheckRs = 0;//定义账号检查结果变量
char scName[MAX_USER_LEN];//定义用户名输入数组变量
char scPwd[MAX_PWD_LEN];//定义密码输入数组变量

//定义用于存储用户信息的结构体
typedef struct {
    char userName[MAX_USER_LEN];
    char userPwd[MAX_PWD_LEN];
} User;
User user[100];

//定义用于存储菜单信息的结构体
typedef struct {
    char menuName[MAX_MENU_LEN];
} Menu;
Menu menu[100];

//定义用于存储图书信息的结构体
typedef struct {
    char isbn[MAX_ISBN_LEN];
    char bookName[MAX_BOOK_LEN];
    char bookPrice[MAX_PRICE_LEN];
    char bookPublisher[MAX_PUBLISHER_LEN];
    char author[MAX_AUTHOR_LEN];
} Book;
Book book[100];


//声明初始化函数
void initData();
int accountCheck(char *name,char *pwd);
void userLogin();
void userReg();
void showMenu();
void bookMag();

void addBook();
void updateBook();
int delBook();
void queryBook();
void exitSys();

//角色权限分配
//出版社管理



//程序主入口函数
int main(){
    initData();
    printf("欢迎使用闽西职业技术学院图书馆\n");
    printf("使用该系统前请先进行登录\n");
    userLogin();
    //登录成功才显示菜单
    if(accountCheckRs == 3){
        bookMag();
    }
    return 0;
}



void userLogin(){
    if(accountCheckRs == 0){
        printf("请输入用户名：\n");
        scanf("%s",scName);
        printf("请输入密码：\n");
        scanf("%s",scPwd);
    }
    //这个判断是避免当密码错误时，还需要输入用户名的问题。
    if(accountCheckRs == 2){
        printf("请输入密码：\n");
        scanf("%s",scPwd);
    }
    accountCheckRs = accountCheck(scName,scPwd);
    if(accountCheckRs == 1){
        printf("登录失败，该用户不存在，请先进行注册\n");
        //前往注册
        userReg();
        return;
    }
    if(accountCheckRs == 2){
        printf("登录失败，密码错误，请重新输入密码\n");
        userLogin();
        return;
    }
    printf("登录成功\n");
}

void userReg(){
    char regName[MAX_USER_LEN];//定义用户名输入数组变量
    char regPwd[MAX_PWD_LEN];//定义密码输入数组变量
    printf("请输入用户名：\n");
    scanf("%s",regName);
    printf("请输入密码：\n");
    scanf("%s",regPwd);
    stpcpy(user[Data.userCount].userName,regName);
    stpcpy(user[Data.userCount].userPwd,regPwd);
    printf("注册成功,请进行登录！\n");
    accountCheckRs = 0;
    userLogin();
}

int accountCheck(char *name,char *pwd){
    //循环数组中的数据
    for (int i = 0; i < sizeof(user) / sizeof(user[0]); ++i){
        //判断是否有当前输入的账号和密码
        if(strcmp(user[i].userName, name) == 0) {
            if(strcmp(user[i].userPwd, pwd) == 0) {
                return 3; // 用户名和密码都正确
            } else {
                return 2; // 密码错误
            }
        }
    }
    return 1;
}

void showMenu(){
    printf("请根据以下菜单进行图书管理：\n");
    for (int i = 0; i < sizeof(menu) / sizeof(menu[0]); ++i){
        printf("%s ",menu[i].menuName);
    }
    printf("\n");
}


void bookMag(){
    printf("请输入需要操作的对应菜单编号：\n");
    showMenu();
    int rs;
    int num;
    scanf("%d", &num);
    switch (num) {
        case 1:
            //新增图书
            addBook();
            break;
        case 2:
            //修改图书
            updateBook();
            break;
        case 3:
            //删除图书
            rs = delBook();
            if(rs == 1){
                printf("删除成功！\n");
            } else{
                printf("删除失败！\n");
            }
            break;
        case 4:
            //查询图书
            queryBook();
            break;
        case 5:
            //退出系统
            exitSys();
            break;
        default:
            printf("不存在该菜单！\n");
            break;
    }
    bookMag();
}

void addBook(){
    char isbn[MAX_ISBN_LEN];
    char bookName[MAX_BOOK_LEN];
    char bookPrice[MAX_PRICE_LEN];
    char bookPublisher[MAX_PUBLISHER_LEN];
    char author[MAX_AUTHOR_LEN];
    printf("请输入图书isbn号：\n");
    scanf("%s",isbn);

    for (int i = 0; i < Data.bookCount; ++i) {
        if(strstr(book[i].isbn,isbn)){
            printf("该图书已存在，请重新输入！\n");
            printf("\n");
            addBook();
            break;
        }
    }

    printf("请输入图书名称：\n");
    scanf("%s",bookName);

    printf("请输入图书价格：\n");
    scanf("%s",bookPrice);

    printf("请输入图书出版社：\n");
    scanf("%s",bookPublisher);

    printf("请输入图书作者：\n");
    scanf("%s",author);

    stpcpy(book[Data.bookCount].isbn,isbn);
    stpcpy(book[Data.bookCount].bookName,bookName);
    stpcpy(book[Data.bookCount].bookPrice,bookPrice);
    stpcpy(book[Data.bookCount].bookPublisher,bookPublisher);
    stpcpy(book[Data.bookCount].author,author);
    Data.bookCount++;
    printf("图书新增成功！");
    printf("您还想进行哪些操作？");

}

void updateBook(){
    //初始化查询出来需要更新数据的索引
    int index;
    //定义临时存储数据的数组
    char isbn[MAX_ISBN_LEN];
    char bookName[MAX_BOOK_LEN];
    char bookPrice[MAX_PRICE_LEN];
    char bookPublisher[MAX_PUBLISHER_LEN];
    char author[MAX_AUTHOR_LEN];

    printf("请输入图书isbn号：\n");
    scanf("%99s",isbn);
    for (int i = 0; i < Data.bookCount; ++i) {
        if(strstr(book[i].isbn,isbn)){
            index = i;//获得需要修改数据的索引
            stpcpy(isbn,book[i].isbn);
            stpcpy(bookName,book[i].bookName);
            stpcpy(bookPrice,book[i].bookPrice);
            stpcpy(bookPublisher,book[i].bookPublisher);
            stpcpy(author,book[i].author);
            printf("图书%d --> ", i+1);
            printf("isbn：%s，", book[i].isbn);
            printf("名称：%s，", book[i].bookName);
            printf("价格：%s，", book[i].bookPrice);
            printf("出版社：%s，", book[i].bookPublisher);
            printf("作者：%s ", book[i].author);
            printf("\n");
            break;
        }
    }

    int flag = 1;
    while (flag){
        printf("%s\n","图书修改有以下操作, 请输入操作码： 1-图书名称修改 2-图书价格修改 3-图书出版社修改 4-图书作者修改 5-退出修改");
        printf("请输入需要修改数据操作码：\n");
        int num;
        scanf("%d", &num);
        switch (num) {
            case 1:
                printf("请输入图书名称：\n");
                scanf("%s",bookName);
                stpcpy(book[index].bookName,bookName);
                break;
            case 2:
                printf("请输入图书价格：\n");
                scanf("%s",bookPrice);
                stpcpy(book[index].bookPrice,bookPrice);
                break;
            case 3:
                printf("请输入图书出版社：\n");
                scanf("%s",bookPublisher);
                stpcpy(book[index].bookPublisher,bookPublisher);
                break;
            case 4:
                printf("请输入图书作者：\n");
                scanf("%s",author);
                stpcpy(book[index].author,author);
                break;
            case 5:
                flag = 0;
                break;
            default:
                break;
        }
        printf("修改成功！\n");
    }
}

int delBook(){
    char isbnName[100];
    printf("请输入图书isbn号：\n");
    scanf("%99s",isbnName);
    for (int i = 0; i < Data.bookCount; i++) {
        if (strcmp(book[i].isbn, isbnName) == 0) {
            // 移动后续元素覆盖当前元素
            for (int j = i; j < Data.bookCount - 1; j++) {
                book[j] = book[j + 1];
            }
            Data.bookCount--;
            return 1; // 删除成功
        }
    }
    return 0; // 未找到
}

void queryBook(){
    int flag = 1;
    while (flag){
        char isbnName[100];
        char tempName[100];
        printf("%s\n","图书查询有以下操作, 请输入操作码： 1-isbn查询 2-查询全部 3-书名查询(模糊) 4-返回上级菜单");
        printf("请输入查询操作码：\n");
        int num;
        scanf("%d", &num);
        switch (num) {
            case 1:
            case 2:
            case 3:
                if(Data.bookCount == 0){
                    printf("暂无图书信息！\n");
                } else{
                    if(num == 1){
                        printf("请输入图书isbn号：\n");
                        scanf("%99s",isbnName);
                        for (int i = 0; i < Data.bookCount; ++i) {
                            if(strstr(book[i].isbn,isbnName)){
                                printf("图书%d --> ", i+1);
                                printf("isbn：%s，", book[i].isbn);
                                printf("名称：%s，", book[i].bookName);
                                printf("价格：%s，", book[i].bookPrice);
                                printf("出版社：%s，", book[i].bookPublisher);
                                printf("作者：%s ", book[i].author);
                                printf("\n");
                                break;
                            }
                        }
                        printf("\n");
                    }
                    if(num == 2){
                        for (int i = 0; i < Data.bookCount; ++i) {
                            printf("图书%d --> ", i+1);
                            printf("isbn：%s，", book[i].isbn);
                            printf("名称：%s，", book[i].bookName);
                            printf("价格：%s，", book[i].bookPrice);
                            printf("出版社：%s，", book[i].bookPublisher);
                            printf("作者：%s ", book[i].author);
                            printf("\n");
                        }
                        printf("\n");
                    }
                    if(num == 3){
                        printf("请输入图书名称：\n");
                        scanf("%99s",tempName);
                        for (int i = 0; i < Data.bookCount; ++i) {
                            if(strstr(book[i].bookName,tempName)){
                                printf("图书%d --> ", i+1);
                                printf("isbn：%s，", book[i].isbn);
                                printf("名称：%s，", book[i].bookName);
                                printf("价格：%s，", book[i].bookPrice);
                                printf("出版社：%s，", book[i].bookPublisher);
                                printf("作者：%s ", book[i].author);
                                printf("\n");
                                break;
                            }
                        }
                        printf("\n");
                    }

                }
                break;
            case 4:
                flag = 0;
                break;
            default:
                break;
        }
    }
}

void exitSys(){
    printf("拜拜！");
    exit(EXIT_SUCCESS);
}

//初始化函数
void initData(){
    stpcpy(user[0].userName,"admin");
    stpcpy(user[0].userPwd,"123456");
    //新增成功后，将数组索引往后挪一位
    Data.userCount++;

    stpcpy(menu[0].menuName,"1-新增");
    stpcpy(menu[1].menuName,"2-修改");
    stpcpy(menu[2].menuName,"3-删除");
    stpcpy(menu[3].menuName,"4-查询");
    stpcpy(menu[4].menuName,"5-退出系统");
    Data.menuCount = 5;//下一个菜单索引从5开始

    stpcpy(book[0].isbn,"9787040601763");
    stpcpy(book[0].bookName,"C语言程序设计项目化教程");
    stpcpy(book[0].bookPrice,"55");
    stpcpy(book[0].bookPublisher,"高等教育出版社");
    stpcpy(book[0].author,"黑马程序员");
    Data.bookCount++;

}
